<?php

namespace App\Repository;


use App\Models\Theme;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
 * Class ThemeRepository
 * @package App\Repository
 */
class ThemeRepository
{
    /**
     * @var Theme
     */
    public $theme;

    /**
     * ThemeRepository constructor.
     * @param Theme $theme
     */
    public function __construct(Theme $theme)
    {
        $this->theme = $theme;
    }


    /**
     * 获取IDS的主题的一组主题
     * @param $ids
     * @return \Illuminate\Database\Eloquent\Collection|static[]
     */
    public function getThemeByIds($ids)
    {
        $result = $this->theme->with('topicImg', 'headImg')->whereIn('id', $ids)->get() ;
        if (!$result){
            throw new NotFoundHttpException('指定的主题不存在, 请检查ID值', null, 30000);
        }
        return $result;
    }

    /**
     * 获取主题下的商品
     * @param $id
     * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|null|static|static[]
     */
    public function getProductsWithTheme($id)
    {
        $result = $this->theme->with('products','topicImg', 'headImg')->find($id);
        if (!$result){
            throw new NotFoundHttpException('请求的ID不存在，请检查ID值', null, 30000);
        }
        return $result;
    }
}